package com.zlp.controller;

import com.zlp.utils.RedisLock;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author ：ZouLiPing
 * @description：支付接口回调
 * @date ： 2019/8/15 14:21
 */
@RestController
@Slf4j
public class PaymentController {

    @Resource
    private RedisLock redisLock;


    @GetMapping("getPayment")
    public String getPayment(){
        String key= "pay_021548794102300016";
        long acuireTimeout = 2000L;
        long timeout = 1000L;
        log.info("key{}",redisLock.getString(key));
        String lock = redisLock.getLock(key, acuireTimeout, timeout);
        if (StringUtils.isBlank(lock)) {
            lock = "该key【" +key+ "】已经被占用了";
            log.info("该key【{}】已经被占用了",key);
        }
        return lock;
    }

    @GetMapping("delPayment")
    public String delPayment(String key,String value){
        String lock = "";
        Boolean delete = redisLock.unLock(key,value);
        if (delete) {
            lock = "该key【" +key+ "】删除成功";
            log.info("该key【{}】被删除成功",key);
        }
        return lock;
    }


}
